import 'dart:async';
import 'package:flutter/material.dart';
import 'package:loadmore/loadmore.dart';
import 'package:xview/components/xNavbar.dart';

class loadMore extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _loadMoreState();
  }
}

class _loadMoreState extends State<loadMore> {
  List list = [];
  List newList = [''];
  int page = 1;
  int limit = 5;
  int count = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: XNavbar(
        title: '加载更多'
      ).render(context),
      body: Container(
        child: LoadMore(
          isFinish: newList.length==0?true:false,
          onLoadMore: getData,
          child: ListView.builder(
            itemCount: count,
            itemBuilder: (context,index){
              return Container(
                height: 100,
                child: Center(
                  child: Text('page-${index+1}'),
                ),
              );
            },
          ),
        ),
      ),
    );
  }

  Future<bool> getData() async{
    // 模拟请求
    await Future.delayed(Duration(milliseconds: 1000),(){
      List data = []; //模拟数据
      if(page <= 5){
        for(var i=1;i<=limit;i++){
          data.add(i+(page-1)*limit);
        }
      }
      setState(() {
        list.addAll(data);
        newList = data;
        page++;
      });
      setState(() {
        count = list.length;
      });
    });
    return true;
  }
}
